1 Imports System.Data.SqlClient
2 Public Class frmSession
3
4 Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
5 Me.Close()
6 End Sub
7
8 Sub Reset()
9 txtSession.Text = ""
10 btnSave.Enabled = True
11 btnDelete.Enabled = False
12 btnUpdate.Enabled = False
13 txtSession.Focus()
14 End Sub
15 Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
16 Reset()
17 End Sub
18
19 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
20 If txtSession.Text = "" Then
21 MessageBox.Show("Please enter Session", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
22 txtSession.Focus()
23 Return
24 End If
25
26 Try
27 con = New SqlConnection(cs)
28 con.Open()
29 Dim ct As String = "select Session from Session_Master where Session=@d1"
30 cmd = New SqlCommand(ct)
31 cmd.Parameters.AddWithValue("@d1", txtSession.Text)
32 cmd.Connection = con
33 rdr = cmd.ExecuteReader()
34
35 If rdr.Read() Then
36 MessageBox.Show("Session Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
37 txtSession.Text = ""
38 txtSession.Focus()
39 If (rdr IsNot Nothing) Then
40 rdr.Close()
41 End If
42 Return
43 End If
44
45 con = New SqlConnection(cs)
46 con.Open()
47
48 Dim cb As String = "insert into Session_Master(Session) VALUES (@d1)"
49 cmd = New SqlCommand(cb)
50 cmd.Parameters.AddWithValue("@d1", txtSession.Text)
51 cmd.Connection = con
52 cmd.ExecuteReader()
53 con.Close()
54 Dim st As String = "added the new Session '" & txtSession.Text & "'"
55 LogFunc(lblUser.Text, st)
56 MessageBox.Show("Successfully Saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
57 btnSave.Enabled = False
58 Getdata()
59 Catch ex As Exception
60 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
61 End Try
62 End Sub
63
64 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
65 If txtSession.Text = "" Then
66 MessageBox.Show("Please enter Session", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
67 txtSession.Focus()
68 Return
69 End If
70
71 Try
72
73 con = New SqlConnection(cs)
74 con.Open()
75
76 Dim cb As String = "Update Session_master set Session=@d1 where Session=@d2"
77 cmd = New SqlCommand(cb)
78 cmd.Connection = con
79 cmd.Parameters.AddWithValue("@d1", txtSession.Text)
80 cmd.Parameters.AddWithValue("@d2", txtSessionName.Text)
81 cmd.ExecuteReader()
82 con.Close()
83 Dim st As String = "updated the Session '" & txtSession.Text & "'"
84 LogFunc(lblUser.Text, st)
85 MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
86 btnUpdate.Enabled = False
87 Getdata()
88 Catch ex As Exception
89 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
90 End Try
91 End Sub
92 Private Sub DeleteRecord()
93
94 Try
95 Dim RowsAffected As Integer = 0
96 con = New SqlConnection(cs)
97 con.Open()
98 Dim cl As String = "select Session_Master.Session from Session_master,Student where Session_Master.Session=Student.Session and Session_master.Session=@d1"
99 cmd = New SqlCommand(cl)
100 cmd.Connection = con
101 cmd.Parameters.AddWithValue("@d1", txtSessionName.Text)
102 rdr = cmd.ExecuteReader()
103 If rdr.Read Then
104 MessageBox.Show("Unable to delete..Already in use in Student Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
105 If Not rdr Is Nothing Then
106 rdr.Close()
107 End If
108 Exit Sub
109 End If
110 con = New SqlConnection(cs)
111 con.Open()
112 Dim cq As String = "delete from Session_master where Session=@d1"
113 cmd = New SqlCommand(cq)
114 cmd.Parameters.AddWithValue("@d1", txtSessionName.Text)
115 cmd.Connection = con
116 RowsAffected = cmd.ExecuteNonQuery()
117 If RowsAffected > 0 Then
118 Dim st As String = "deleted the Session '" & txtSession.Text & "'"
119 LogFunc(lblUser.Text, st)
120 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
121 Getdata()
122 Reset()
123 Else
124 MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
125 Reset()
126 End If
127 If con.State = ConnectionState.Open Then
128 con.Close()
129
130 End If
131 Catch ex As Exception
132 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
133 End Try
134 End Sub
135 Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
136 Try
137 If MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
138 DeleteRecord()
139 End If
140 Catch ex As Exception
141 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
142 End Try
143 End Sub
144
145 Private Sub dgw_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
146 Try
147 Dim dr As DataGridViewRow = dgw.SelectedRows(0)
148 txtSessionName.Text = dr.Cells(0).Value.ToString()
149 txtSession.Text = dr.Cells(0).Value.ToString()
150 btnUpdate.Enabled = True
151 btnDelete.Enabled = True
152 btnSave.Enabled = False
153 Catch ex As Exception
154 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
155 End Try
156 End Sub
157
158 Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
159 Dim strRowNumber As String = (e.RowIndex + 1).ToString()
160 Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
161 If dgw.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then
162 dgw.RowHeadersWidth = Convert.ToInt32((size.Width + 20))
163 End If
164 Dim b As Brush = SystemBrushes.ControlText
165 e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
166
167 End Sub
168 Public Sub Getdata()
169 Try
170 con = New SqlConnection(cs)
171 con.Open()
172 cmd = New SqlCommand("SELECT RTRIM(Session) from Session_Master order by Session", con)
173 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
174 dgw.Rows.Clear()
175 While (rdr.Read() = True)
176 dgw.Rows.Add(rdr(0))
177 End While
178 con.Close()
179 Catch ex As Exception
180 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
181 End Try
182 End Sub
183
184 Private Sub frmtype_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
185 Getdata()
186 End Sub
187 End Class